<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\Inspire::class,
        Commands\TcpServer::class,
        Commands\OfflineRecharge::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->everyMinute();
                 
        $schedule->exec('nohup php artisan swoole:tcpserver start >storage/logs/tcpserver.error.log 2>&1 &')->everyMinute()->when(function(){
            return exec('ps -aux|grep swoole:tcpserver|grep -v "grep"|wc -l') < 4;
        });
        $schedule->exec('nohup node app/App.js >storage/logs/nodejs.log 2>&1 &')->everyMinute()->when(function(){
            return exec('ps -aux|grep "node app/App.js"|grep -v "grep"|wc -l') < 1;
        });
        $schedule->exec('nohup php artisan queue:work --daemon --tries=3 --queue=queue --sleep=0 >/dev/null 2>>storage/logs/queue.log &')->everyMinute()->when(function(){
            return exec('ps aux|grep "artisan queue:work --daemon --tries=3 --queue=queue --sleep=0"|grep -v "grep"|wc -l') < 1;
        });
        $schedule->exec('nohup php artisan queue:work --daemon --tries=3 --delay=20 --queue=commandRepeater >/dev/null 2>>storage/logs/queue.log &')->everyMinute()->when(function(){
            return exec('ps aux|grep "artisan queue:work --daemon --tries=3 --delay=20 --queue=commandRepeater"|grep -v "grep"|wc -l') < 1;
        });

        $schedule->command('offline-recharge')
                 ->everyMinute();

        $schedule->command('backup:clean')->daily()->at('01:00');
        $schedule->command('backup:run --only-db')->daily()->at('02:00');

    }
}
